package com.lei2j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@RestController
public class FeignApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(FeignApplication.class,args);
    }

    //调用远程服务,断路器fallback失败回调方法
    @FeignClient(value = "client",fallback = TestServiceImpl.class)
    public interface TestService {

        @RequestMapping(value = "/test",method = {RequestMethod.GET})
        String get(@RequestParam("name") String name);
    }

    @Service
    public class TestServiceImpl implements TestService{

        public String get(String name) {
            return "client error";
        }
    }

    @Autowired
    private TestService testService;

    @RequestMapping("/feign/test")
    public String get(String name){
        return testService.get(name);
    }
}
